/**
 * @filename:ReceiveController Jan 14, 2025
 * @project Wonder Vue Admin Api  1.1.0
 * Copyright(c) 2020 Wonder Co. Ltd. 
 * All right reserved. 
 */
package com.wonder.app.controller;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Date;
import java.util.Map;

import cn.hutool.json.JSONObject;
import com.vtuzx.core.bean.dto.VtuzxMap;
import com.wonder.admin.domain.SysAdmin;
import com.wonder.admin.service.SysAdminService;
import com.wonder.app.domain.Receive;
import com.wonder.app.service.MoldMaterialService;
import com.wonder.app.service.ReceiveService;
import com.wonder.app.request.ReceiveRequest;
import com.wonder.app.service.StkApplyService;
import com.wonder.app.service.StkweightService;
import com.wonder.constant.IXqcConst;
import com.wonder.framework.authorization.CheckAdminToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.wonder.common.common.AjaxMap;
import com.wonder.common.common.AjaxResult;
import com.wonder.common.common.CommonPage;
import com.wonder.common.utils.MessageUtils;
import com.wonder.common.utils.StringUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;

import javax.servlet.http.HttpServletRequest;

/**
 * 
 * <p>说明： 收料通知单API接口层</P>
 * @version: 1.1.0
 * @author: Wonder
 * @time    Jan 14, 2025
 *
 */
@Api(tags = "收料通知单")
@RestController
@RequestMapping("/app/Receive")
public class ReceiveController {
	
	@Autowired
	private ReceiveService receiveService;

	@Autowired
	private StkweightService stkweightService;

	@Autowired
	private SysAdminService adminService;

	@Autowired
	private CheckAdminToken checkAdminToken;

	@Autowired
	private MoldMaterialService moldMaterialService;

	@Autowired
	private StkApplyService stkApplyService;
	
	@ApiOperation(value = "收料通知单列表")
	@RequestMapping(value = "/index", method = RequestMethod.POST)
	public Map<String,Object> index(@RequestBody ReceiveRequest receiveRequest) {
		Page<Receive> page = new Page<Receive>(receiveRequest.getPage(), receiveRequest.getLimit());
		IPage<Receive> list = receiveService.selectList(receiveRequest, page);
		
		Map<String,Object> retMap=new HashMap<>();
		retMap.put("data",CommonPage.restPage(list));
		
  
		return AjaxMap.success(retMap);
	}
	
	/**
	 * @explain 修改排序开关
	 * @param   entity
	 * @return  Boolean
	 * @author  Wonder
	 */
	@PostMapping("/updateExt")
	@ApiOperation(value = "修改排序开关", notes = "作者：Wonder")
	public AjaxResult<Receive> updateExt(@RequestBody Receive entity){
		if (null!=entity) {
			boolean rsg = receiveService.updateById(entity);
			if (rsg) {
				  return AjaxResult.success("system.updateSuccess");
			  }else {
				  return AjaxResult.failed("system.updateFailed");
			  }
		}else {
			return AjaxResult.failed("system.parameterError");
		}
	}
	
	/**
	 * @explain 添加
	 * @param
	 * @return  Boolean
	 * @author  Wonder
	 */
	@PostMapping("/add")
	@ApiOperation(value = "添加", notes = "作者：Wonder")
	public AjaxResult<?> add(HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception{
		String token = checkAdminToken.getTokenFormRequest(request);
		SysAdmin user = adminService.getAdminByToken(token);
		String userName = user.getName();
		Boolean res = receiveService.createReceive(userName,map);
		if (res){
			return AjaxResult.success(res);
		} else {
			return AjaxResult.failed();
		}

	}
	
	/**
	 * @explain 查询对象  <swagger GET请求>
	 * @param   map 对象参数：id
	 * @return  JsonResult
	 * @author  Wonder
	 */
	@PostMapping("/getUpdateInfo")
	@ApiOperation(value = "获取收料通知单对象", notes = "作者：Wonder")
	public AjaxResult<Receive> getUpdateInfo(@RequestBody Map<String,Object> map){
	    if(map==null) return AjaxResult.failed("system.parameterError");
		Long Id=Long.parseLong(map.get("Id").toString());		
		 Receive receive=receiveService.getById(Id);
		
		if (null!=receive ) {
			 return AjaxResult.success(receive);
		}else {
			 return AjaxResult.failed("system.parameterError");
		}
		
	}
	
	/**
	 * @explain 修改
	 * @param   entity
	 * @return  Boolean
	 * @author  Wonder
	 */
	@PostMapping("/update")
	@ApiOperation(value = "修改", notes = "作者：Wonder")
	public AjaxResult<Receive> update(@RequestBody Receive entity){
		if (null!=entity) {
		    String validate=receiveService.validate(entity,"update");
		    if(StringUtils.hasText(validate))
		         return AjaxResult.failed(validate);
			boolean rsg = receiveService.updateById(entity);
			if (rsg) {
				  return AjaxResult.success("system.updateSuccess");
			  }else {
				  return AjaxResult.failed("system.updateFailed");
			  }
		}else {
			return AjaxResult.failed("system.parameterError");
		}
	}
	
	/**
	 * @explain 删除
	 * @param   map 对象参数：id
	 * @return  JsonResult
	 * @author  Wonder
	 */
	@PostMapping("/delete")
	@ApiOperation(value = "删除", notes = "作者：Wonder")
	public AjaxResult<Receive> delete(@RequestBody Map<String,Object> map){
		if(map==null)return AjaxResult.failed("system.parameterError");
		String ids=map.get("Id").toString();
		ids=StringUtils.remove(ids, '[');
		ids=StringUtils.remove(ids, ']');
		String[] deleteIds=StringUtils.split(ids, ',');
		List<Long> idList=new ArrayList<>(); 
		for(int i=0;i<deleteIds.length;i++)
		{
			idList.add( Long.parseLong(deleteIds[i].trim()));//整数数组
		}

	    boolean rsg = receiveService.removeBatchByIds(idList);
	    if (rsg) {
		    return AjaxResult.success("system.deleteSuccess");
	    }else {
		   return AjaxResult.failed("system.deleteFailed");
	    }
		
	}

	/**
	 * @explain 查看对象明细
	 * @param   map 对象参数：id
	 * @return  JsonResult
	 * @author  Wonder
	 */
	@ApiOperation(value = "查询Receive明细")
	@ResponseBody
	@PostMapping(value = "/receivedetail")

	public AjaxResult<Map<String,Object>> getReDetail(HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception{
		if (null == map){
			return AjaxResult.failed("system.parameterError");
		}
		String token = checkAdminToken.getTokenFormRequest(request);
		SysAdmin user = adminService.getAdminByToken(token);
		String userName = user.getName();
		Long user_id = user.getUser_id();
		Map<String,Object> data = receiveService.getReDetail(user_id,map);
		if (null != data){
			return AjaxResult.success(data);
		} else {
			return AjaxResult.failed();
		}

	}

	/**
	 * @explain 查看对象明细(手机端)
	 * @param   map 对象参数：id
	 * @return  JsonResult
	 * @author  Wonder
	 */
	@ApiOperation(value = "查询Receive明细(手机端)")
	@ResponseBody
	@PostMapping(value = "/get_receive_detail")

	public AjaxResult<Map<String,Object>> getReceiveDetail(HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception{
		if (null == map){
			return AjaxResult.failed("system.parameterError");
		}
		String token = checkAdminToken.getTokenFormRequest(request);
		SysAdmin user = adminService.getAdminByToken(token);
		String userName = user.getName();
		Long user_id = user.getUser_id();
		Map<String,Object> data = receiveService.getReceiveDetail(user_id,map);
		if (null != data){
			return AjaxResult.success(data);
		} else {
			return AjaxResult.failed();
		}

	}


	@ApiOperation(value = "收料通知单列表(手机端)")
	@RequestMapping(value = "/get_receive_list_phone", method = RequestMethod.POST)
	public AjaxResult<Map<String,Object>> getReceiveListPhone(@RequestBody Map<String,Object> map) throws Exception{
		if(null == map){
			return AjaxResult.failed("system.parameterError");
		}

		Map<String,Object> data = receiveService.getReceiveListPhone(map);
		if (null != data){
			return AjaxResult.success(data);
		} else {
			return AjaxResult.failed();
		}
	}

	/**
	 * 采购第一次称重
	 *
	 * @param action
	 * @param bh
	 * @return
	 * @throws Exception
	 */
	@ApiOperation(value = "采购第一次称重")
	@ResponseBody
	@PostMapping(value = "/fir_purweight")
	public Map<String,Object> firpurweight(@RequestParam(name = "action") String action, @RequestParam(name = "bh") String bh) throws Exception {
		boolean result = false;
		if (bh.substring(0,1).equals("B")) {
			result = stkweightService.firStkWeight(bh.substring(1));
		}else {
			 result = receiveService.firpurweight(bh);
		}
		// 构造 form 数据
		Map<String, Object> form = new HashMap<>();
		form.put("bh", 0); // 示例值
		form.put("dm", "");
		form.put("mc", "");
		form.put("menuId", 0);

		// 构造返回数据
		Map<String, Object> response = new HashMap<>();
		response.put("form", form);
		if (result == true) {
			response.put("msg", "操作成功");
		} else {
			response.put("msg", "未查询到数据");
		}
		response.put("result", result);

		return response;
	}


	/**
	 * 采购第二次称重
	 *
	 * @param action
	 * @param bh
	 * @return
	 * @throws Exception
	 */
	@ApiOperation(value = "采购第二次称重")
	@ResponseBody
	@PostMapping(value = "/sec_purweight")
	public Map<String,Object> secpurweight(@RequestParam(name = "action") String action, @RequestParam(name = "bh") String bh, @RequestParam(name = "sl") String sl) throws Exception {
		VtuzxMap result = new VtuzxMap();
		if (bh.substring(0,1).equals("B") || bh.substring(bh.length() - 1).equals("B")) {
			result = stkweightService.stkSecWeight(bh.replaceAll("[^0-9]", ""),sl);
		}else {
			result = receiveService.purweight(bh, sl);
		}
		Map<String, Object> form = new HashMap<>();
		form.put("bh", result.getInt("bh")); // 示例值
		form.put("dm", result.getString("BillNo"));
		form.put("mc", "");
		form.put("menuId", 0);

		// 构造返回数据
		Map<String, Object> response = new HashMap<>();
		response.put("form", form);
		response.put("msg", result.getString("msg"));
		response.put("result", result.getBoolean("result"));
		return  response;
	}

	/**
	 * @explain 金蝶审核收料通知单接口
	 * @param
	 * @return  Boolean
	 * @author  Wonder
	 */
	@PostMapping("/audit")
	@ApiOperation(value = "审核", notes = "作者：Wonder")
	public AjaxResult<Boolean> audit(@RequestBody JSONObject map) throws Exception{
		if (null != map) {
			boolean res = receiveService.audit(map);
			if (res){
				return AjaxResult.success(res);
			} else {
				return AjaxResult.failed();
			}
		}else {
			return AjaxResult.failed("system.parameterError");
		}
	}


	/**
	 * @explain 金蝶反审核收料通知单接口
	 * @param
	 * @return  Boolean
	 * @author  Wonder
	 */
	@PostMapping("/unaudit")
	@ApiOperation(value = "反审核", notes = "作者：Wonder")
	public AjaxResult<Boolean> unAudit(@RequestBody JSONObject map) throws Exception{
		if (null != map) {
			boolean res = receiveService.unAudit(map);
			if (res){
				return AjaxResult.success(res);
			} else {
				return AjaxResult.failed();
			}
		}else {
			return AjaxResult.failed("system.parameterError");
		}
	}

	/**
	 * @explain 收料通知单行关闭
	 * @param
	 * @return  Boolean
	 * @author  Wonder
	 */
	@PostMapping("/receive_close")
	@ApiOperation(value = "收料通知单行关闭", notes = "作者：Wonder")
	public AjaxResult<Boolean> receiveClose(@RequestBody JSONObject map) throws Exception{
		if (null != map) {
			boolean res = receiveService.receiveClose(map);
			if (res){
				return AjaxResult.success(res);
			} else {
				return AjaxResult.failed();
			}
		}else {
			return AjaxResult.failed("system.parameterError");
		}
	}

	@ApiOperation(value = "查询仓库")
	@RequestMapping(value = "/get_stock_list", method = RequestMethod.POST)
	public AjaxResult<?> getStockList(HttpServletRequest request,@RequestBody Map<String,Object> map) throws Exception{
		String token = checkAdminToken.getTokenFormRequest(request);
		SysAdmin user = adminService.getAdminByToken(token);
		String userName = user.getName();
		Long user_id = user.getUser_id();

		Map<String,Object> data = receiveService.getStockList(user_id,map);
		if (null != data){
			return AjaxResult.success(data);
		} else {
			return AjaxResult.failed();
		}
	}

	/**
	 * @explain 金蝶其他出库单传过来模具
	 * @param
	 * @return  Boolean
	 * @author  Wonder
	 */
	@PostMapping("/audit_mold")
	@ApiOperation(value = "审核", notes = "作者：Wonder")
	public AjaxResult<Boolean> auditMold(@RequestBody JSONObject map) throws Exception{
		if (null != map) {
			boolean res = moldMaterialService.audit(map);
			if (res){
				return AjaxResult.success(res);
			} else {
				return AjaxResult.failed();
			}
		}else {
			return AjaxResult.failed("system.parameterError");
		}
	}


}